/* global qq */

// --QQ地图定位--
export default {
  loadQQmap: function (mapconfig, successFun, errorFun, isMap) {
    const _this = this
    if (isMap) {
      // _this.showQQMap(mapconfig)
      _this.showTMap(mapconfig)
    } else {
      const mapsc = document.createElement('script')
      mapsc.type = 'text/javascript'
      if (mapsc.readyState) {
        mapsc.onreadystatechange = function () {
          if (this.readyState === 'loaded' || this.readyState === 'complete') {
            _this.getMyLocation(mapconfig, successFun, errorFun, mapsc)
            mapsc.onreadystatechange = null
          }
        }
      } else {
        mapsc.onload = function () {
          _this.getMyLocation(mapconfig, successFun, errorFun, mapsc)
          mapsc.onload = null
        }
      }
      mapsc.src = 'https://3gimg.qq.com/lightmap/components/geolocation/geolocation.min.js'
      document.getElementsByTagName('head')[0].insertBefore(mapsc, document.getElementsByTagName('head')[0].firstChild)
    }
  },
  getMyLocation: function (mapconfig, successFun, errorFun, forObj) {
    setTimeout(function () {
      document.getElementsByTagName('head')[0].removeChild(forObj)
    }, mapconfig.qqTimeOut.timeout)
    // ----
    const geolocate = new qq.maps.Geolocation(mapconfig.qqKey, mapconfig.appName)
    geolocate.getLocation(showPosition, showErr, mapconfig.qqTimeOut)
    function showPosition(position) {
      successFun && successFun(position)
    }
    function showErr() {
      errorFun && errorFun(mapconfig.qqMapErr)
    }
  },
  showQQMap: function (mapconfig) {
    const map = new qq.maps.Map(mapconfig.refObj, { zoom: 18 })
    // --调用地址解析类--
    const geocoder = new qq.maps.Geocoder({
      complete: function (result) {
        // --设置地图初始化坐标--
        map.setCenter(result.detail.location)
        // --设置地图自定义标记--
        const anchor = new qq.maps.Point(6, 6)
        const size = new qq.maps.Size(30, 30)
        const origin = new qq.maps.Point(0, 0)
        const icon = new qq.maps.MarkerImage(mapconfig.icon, size, origin, anchor)
        const marker = new qq.maps.Marker({
          icon: icon,
          map: map,
          position: result.detail.location
        })
        // --设置地图弹出层--
        const infoWin = new qq.maps.InfoWindow({ map: map })
        infoWin.open()
        infoWin.setContent(mapconfig.infoBox)
        infoWin.setPosition(result.detail.location)
        // --侦听标记点击事件--
        qq.maps.event.addListener(marker, 'click', function () {
          infoWin.open()
        })
      }
    })
    geocoder.getLocation(mapconfig.address)
  },
  //根据地址获取经纬度
  getLal: (address: string, cb: Function) => {
    var geocoder = new qq.maps.Geocoder();
    geocoder.getLocation(address);
    geocoder.setComplete(function (res) {
      cb(res)
    });
    geocoder.setError(function (err) {
      cb(err)
    });
  },
  showTMap: function (mapconfig) {
    let center = new TMap.LatLng(30.590837, 114.306281);//设置中心点坐标 武汉
    //初始化地图
    const map = new TMap.Map(mapconfig.refObj, {
      // center: center,
      zoom: 18
    });

    const geocoder = new TMap.service.Geocoder(); // 新建一个正逆地址解析类
    // 将给定的地址转换为坐标位置
    geocoder.getLocation({ address: mapconfig.address }).then((result) => {
      // console.log(result.result.location);
      let location = result.result.location;
      center = new TMap.LatLng(location.lat, location.lng);
      map.setCenter(center)
      // --设置地图自定义标记--
      //初始化marker
      var marker = new TMap.MultiMarker({
        id: "marker-layer", //图层id
        map: map,
        styles: { //点标注的相关样式
          "marker": new TMap.MarkerStyle({
            "width": 25,
            "height": 35,
            // "anchor": { x: 6, y: 32 },
            // "src": mapconfig.icon
          })
        },
        geometries: [{ //点标注数据数组
          "id": "demo",
          "styleId": "marker",
          "position": center,
          // "properties": {
          //   "title": "marker"
          // }
        }]
      });
      // 为点标记添加点击事件监听
      marker.on("click", function (evt) {
        // console.log(evt)
        infoWindow.open(); //打开信息窗
        // infoWindow.setPosition(evt.geometry.position);//设置信息窗位置
        // //将信息与标记点关联起来
        // infoWindow.setContent(evt.geometry.properties.title);//设置信息窗内容
      })

      //初始化infoWindow
      var infoWindow = new TMap.InfoWindow({
        map: map,
        position: center, //设置信息框位置
        offset: { x: 0, y: -32 },
        content: mapconfig.infoBox //设置信息框内容
      });

    });


  }
}
